<template>
  <div class="password-container">
    <n-card class="password-card" title="考勤管理系统" :bordered="false">
      <template #header-extra>
        <n-text depth="3">修改密码</n-text>
      </template>

      <n-form
        ref="formRef"
        :model="formValue"
        :rules="rules"
        label-placement="left"
        label-width="0"
        require-mark-placement="right-hanging"
        size="large"
      >
        <n-form-item path="oldPassword">
          <n-input
            v-model:value="formValue.oldPassword"
            type="password"
            show-password-on="click"
            placeholder="请输入当前密码"
          >
            <template #prefix>
              <n-icon :component="LockClosedOutline" />
            </template>
          </n-input>
        </n-form-item>

        <n-form-item path="newPassword">
          <n-input
            v-model:value="formValue.newPassword"
            type="password"
            show-password-on="click"
            placeholder="请输入新密码"
          >
            <template #prefix>
              <n-icon :component="LockClosedOutline" />
            </template>
          </n-input>
        </n-form-item>

        <n-form-item path="confirmPassword">
          <n-input
            v-model:value="formValue.confirmPassword"
            type="password"
            show-password-on="click"
            placeholder="请确认新密码"
          >
            <template #prefix>
              <n-icon :component="LockClosedOutline" />
            </template>
          </n-input>
        </n-form-item>
      </n-form>

      <div style="margin-top: 24px">
        <n-button
          type="primary"
          size="large"
          block
          :loading="loading"
          @click="handleUpdatePassword"
        >
          确认修改
        </n-button>
      </div>

      <div class="tips" style="margin-top: 16px; text-align: center;">
        <n-button text @click="router.push('/dashboard')">
          返回首页
        </n-button>
      </div>
    </n-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useMessage } from 'naive-ui'
import { LockClosedOutline } from '@vicons/ionicons5'
import { updatePassword } from '../../api/auth'

const router = useRouter()
const message = useMessage()
const formRef = ref(null)
const loading = ref(false)

const formValue = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const validatePassword = (rule, value) => {
  if (!value) {
    return new Error('请输入密码')
  }
  if (value.length < 6) {
    return new Error('密码不能少于6个字符')
  }
  return true
}

const validateConfirmPassword = (rule, value) => {
  if (!value) {
    return new Error('请确认新密码')
  }
  if (value !== formValue.newPassword) {
    return new Error('两次输入密码不一致')
  }
  return true
}

const rules = {
  oldPassword: [
    { required: true, message: '请输入当前密码', trigger: 'blur' }
  ],
  newPassword: [
    { validator: validatePassword, trigger: 'blur' }
  ],
  confirmPassword: [
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

const handleUpdatePassword = () => {
  if (!formRef.value) return
  
  formRef.value.validate(async (errors) => {
    if (!errors) {
      loading.value = true
      try {
        const { oldPassword, newPassword, confirmPassword } = formValue
        await updatePassword({ oldPassword, newPassword, confirmPassword })
        message.success('密码修改成功')
        router.push('/dashboard')
      } catch (error) {
        console.error('密码修改失败:', error)
        message.error(error.message || '密码修改失败，请重试')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.password-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
}

.password-card {
  width: 400px;
  border-radius: 16px;
}

:deep(.n-card-header) {
  text-align: center;
  font-size: 22px;
  font-weight: bold;
}
</style> 